package com.zhan.ielts.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class SendSMSSelf {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String phone = "15800372835";
		String content = "this is a test";
		String token = "8a90e80cc7b6b1dedfe3f32a4ddaf5ad";
		// String ret = sendSMS(phone, content, token);
		// System.out.println(ret);
		//
		// if (ret.indexOf("失败") < 0) {
		// System.out.println("成功发送sms");
		// } else {
		// System.out.println("失败发送");
		// }

	}

	public static boolean sendSMS(String phone, String content, String token) {
		HttpClient httpclient = new DefaultHttpClient();
		String smsUrl = "http://sms.service.zhan.com/send";
		HttpPost httppost = new HttpPost(smsUrl);
		String strResult = "";

		boolean flag = false;

		try {

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			JSONObject jobj = new JSONObject();
			jobj.put("phone", phone);
			jobj.put("content", content);
			jobj.put("token", token);

			nameValuePairs.add(new BasicNameValuePair("msg", getStringFromJson(jobj)));

			nameValuePairs.add(new BasicNameValuePair("phone", phone));
			nameValuePairs.add(new BasicNameValuePair("content", content));
			nameValuePairs.add(new BasicNameValuePair("token", token));

			httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

			HttpResponse response = httpclient.execute(httppost);

			if (response.getStatusLine().getStatusCode() == 200) {
				/* 读返回数据 */
				String conResult = EntityUtils.toString(response.getEntity());
				JSONObject sobj = new JSONObject();
				sobj = sobj.fromObject(conResult);
				String result = sobj.getString("status");
				String code = sobj.getString("msg");
				if (result.equals("1")) {
					strResult += "发送成功";
					flag = true;
				} else {
					strResult += "发送失败，" + code;
				}

			} else {
				String err = response.getStatusLine().getStatusCode() + "";
				strResult += "发送失败:" + err;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// return strResult;
		return flag;
	}

	private static String getStringFromJson(JSONObject adata) {
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		for (Object key : adata.keySet()) {
			sb.append("\"" + key + "\":\"" + adata.get(key) + "\",");
		}
		String rtn = sb.toString().substring(0, sb.toString().length() - 1) + "}";
		return rtn;
	}
}